package com.example.Android_test.helpers;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.Android_test.activity.Main;
import com.example.Android_test.specs.LanguageSpec;
import com.example.Android_test.util.LanguageUtils;
import com.example.Android_test.util.TrainingUtils;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: dns
 * Date: 18.05.13
 * Time: 10:49
 * To change this template use File | Settings | File Templates.
 */
public class SettingsHelper {
    public static final SettingsHelper INSTANCE = new SettingsHelper();

    public static final String APP_PREFERENCES_NAME = "mysettings"; // это будет именем файла настроек

    public static final String KEY_EXPERIENCE = "experience";

    public static final String KEY_CURRENT_WORD_SET_ID = "currentWordSetID";
    public static final String KEY_CURRENT_TRAINING_ID = "currentTraining";

    //ETR|TW|PUWIC|GD|SPRINT
    public static final String KEY_TRAINING_TIME_SPENT_ETR = "timeSpentETR";
    public static final String KEY_TRAINING_TIME_SPENT_TW = "timeSpentTW";
    public static final String KEY_TRAINING_TIME_SPENT_PUWIC = "timeSpentPUWIC";
    public static final String KEY_TRAINING_TIME_SPENT_SPRINT = "timeSpentSPRINT";
    public static final String KEY_TRAINING_TIME_SPENT_GD = "timeSpentGD";

    public static final String KEY_IS_FIRST_RUN_TRAINING_ETR = "isFirstRunETR";
    public static final String KEY_IS_FIRST_RUN_TRAINING_TW = "isFirstRunTW";
    public static final String KEY_IS_FIRST_RUN_TRAINING_PUWIC = "isFirstRunPUWIC";
    public static final String KEY_IS_FIRST_RUN_TRAINING_SPRINT = "isFirstRunSPRINT";
    public static final String KEY_IS_FIRST_RUN_TRAINING_GD = "isFirstRunGD";

    public static final String KEY_HOME_LANGUAGE = "homeLanguage";
    public static final String KEY_TARGET_LANGUAGE = "targetLanguage";

    private Hashtable<String, String> preferencesTable = new Hashtable<String, String>();
    //private ArrayList<LanguageSpec> availableLanguages = new ArrayList<LanguageSpec>();

    private SettingsHelper() {
        if (isPreferencesEmpty()) {
            saveDefaultPreferences();
        }

        loadPreferences();
    }

    private void saveDefaultPreferences() {
        SharedPreferences sharedPreferences = Main.getAppContext().getSharedPreferences(APP_PREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();

        editor.putString(KEY_CURRENT_WORD_SET_ID, "");
        editor.putString(KEY_CURRENT_TRAINING_ID, TrainingUtils.ID_TRAINING_ETR);

        editor.putString(KEY_TRAINING_TIME_SPENT_ETR, "0");
        editor.putString(KEY_TRAINING_TIME_SPENT_TW, "0");
        editor.putString(KEY_TRAINING_TIME_SPENT_PUWIC, "0");
        editor.putString(KEY_TRAINING_TIME_SPENT_SPRINT, "0");
        editor.putString(KEY_TRAINING_TIME_SPENT_GD, "0");

        editor.putString(KEY_IS_FIRST_RUN_TRAINING_ETR, "1"); // true
        editor.putString(KEY_IS_FIRST_RUN_TRAINING_TW, "1");
        editor.putString(KEY_IS_FIRST_RUN_TRAINING_PUWIC, "1");
        editor.putString(KEY_IS_FIRST_RUN_TRAINING_SPRINT, "1");
        editor.putString(KEY_IS_FIRST_RUN_TRAINING_GD, "1");

        editor.commit();
    }

    private void loadPreferences() {
        SharedPreferences sharedPreferences = Main.getAppContext().getSharedPreferences(APP_PREFERENCES_NAME, Activity.MODE_PRIVATE);

        Map<String,?> keys = sharedPreferences.getAll();
        for(Map.Entry<String,?> entry : keys.entrySet()){
            preferencesTable.put(entry.getKey(), entry.getValue().toString());
        }
    }

    private boolean isPreferencesEmpty() {
        //TODO uncomment for debug
        //if (true) return true;

        Context context = Main.getAppContext();
        SharedPreferences sharedPreferences = context.getSharedPreferences(APP_PREFERENCES_NAME, Activity.MODE_PRIVATE);

        if (sharedPreferences.getString(KEY_CURRENT_WORD_SET_ID, null) == null &&
                sharedPreferences.getString(KEY_CURRENT_TRAINING_ID, null) == null) {
            return true;
        }

        return false;
    }

    public synchronized ArrayList<LanguageSpec> getAvailableLanguages() {
        return LanguageUtils.getAvailableLanguages();
    }

    public synchronized LanguageSpec getCurrentLanguage() {
        return LanguageUtils.getCurrentLanguage();
    }

    public synchronized String getStringPreference(String key) {
         return preferencesTable.get(key);
    }
    /* public synchronized Integer getIntPreference(String key) {
        return (Integer) preferencesTable.get(key);
    }
    public synchronized Boolean getBooleanPreference(String key) {
        return (Boolean) preferencesTable.get(key);
    }
*/
    /* public synchronized void setIntPreference(String key, Integer value) {
        preferencesTable.put(key, value);
        savePreferences();
    }*/
    public synchronized void setStringPreference(String key, String value) {
        preferencesTable.put(key, value);
        savePreferences();
    }
    /*  public synchronized void setBooleanPreference(String key, Boolean value) {
        preferencesTable.put(key, value);
        savePreferences();
    }*/

    protected void savePreferences() {
        SharedPreferences sharedPreferences = Main.getAppContext().getSharedPreferences(APP_PREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();

        for (String key: preferencesTable.keySet()) {
            editor.putString(key, preferencesTable.get(key));
            /*if (preferencesTable.get(key) instanceof String) {
                editor.putString(key, (String) preferencesTable.get(key));
                continue;
            }
            if (preferencesTable.get(key) instanceof Integer) {
                editor.putInt(key, (Integer) preferencesTable.get(key));
                continue;
            }*/
        }

        editor.commit();
    }

    public synchronized boolean isSOWSelected() {
        if (getStringPreference(SettingsHelper.KEY_CURRENT_WORD_SET_ID) == null ||
                getStringPreference(SettingsHelper.KEY_CURRENT_WORD_SET_ID).isEmpty()) {
            return false;
        }

        return true;
    }

    public synchronized boolean isTrainingSelected() {
        if (getStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID) == null ||
                getStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID).isEmpty()) {
            return false;
        }

        return true;
    }

}
